package com.touni.lambda;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @ClassName : CollectionsDemo
 * @Description :
 * @Author : lwq
 * @Date: 2020-11-04 19:00
 */
public class CollectionsDemo {

    public static void main(String[] args) {
        //shouJiQi();
        diyCollector();
    }

    public static void diyCollector(){
        /*Set<BaseModel> modelSet = baseList.stream().collect(new ToSetCollector<BaseModel>());
        System.out.println(modelSet);*/
        HashSet<Object> collect = baseList.stream().collect(HashSet::new, Set::add, (set1, set2) -> set1.add(set2));
        System.out.println(collect);
        //===以上两个方法  效果相同   只是第二个collect方法不能传递任何Characteristics，所以它永远都是一个IDENTITY_FINISH和
        //CONCURRENT但并非UNORDERED的收集器。137




    }

    public static boolean isPrime(int candidate){
        return IntStream.range(2, candidate).noneMatch(i -> candidate % i == 0);
    }

    /**
     * 收集器： 主要功能
     *    将流元素归约和汇总为一个值
     *    元素分组
     *    元素分区
     */
    public static void shouJiQi(){
        //分区
        /*Map<Boolean, List<BaseModel>> partitionBy = baseList.stream().collect(Collectors.partitioningBy(baseModel -> {
            if (baseModel.getAge() > 30) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        }));
        System.out.println(partitionBy);*/
        Map<Boolean, BaseModel> collect = baseList.stream().collect(Collectors.partitioningBy(baseModel -> {
            if (baseModel.getAge() > 30) {
                return true;
            } else {
                return false;
            }
        }, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(BaseModel::getAge)), Optional::get)));
        System.out.println(collect);
        //分组
        /*Map<Integer, List<BaseModel>> groupingCollect = baseList.stream().collect(Collectors.groupingBy(BaseModel::getAge));
        System.out.println(groupingCollect);*/
        //按照年龄段进行分组
        /*Map<String, List<BaseModel>> paragraphGroupBy = baseList.stream().collect(Collectors.groupingBy(a -> {
            if (a.getAge() < 30) {
                return "age<30";
            } else {
                return "age>30";
            }
        }));
        System.out.println(paragraphGroupBy);*/
        //多级分组
        /*Map<String, Map<String, List<BaseModel>>> multistageGroupBy = baseList.stream().collect(
                Collectors.groupingBy(BaseModel::getSex, Collectors.groupingBy(a -> {
                            if (a.getAge() > 30) {
                                return "age>30";
                            } else {
                                return "age<30";
                            }
        })));
        System.out.println(multistageGroupBy);*/
        //分组2
        /*Map<String, Long> collect = baseList.stream().collect(Collectors.groupingBy(
                BaseModel::getSex, Collectors.counting()));
        System.out.println(collect);*/
        //分组3
        /*Map<String, Optional<BaseModel>> maxAgeBySex = baseList.stream().collect(Collectors.groupingBy(
                BaseModel::getSex, Collectors.maxBy(Comparator.comparing(BaseModel::getAge))));
        System.out.println(maxAgeBySex);*/
        //分组4  collectingAndThen 用于 转换收集器的结果类型
        /*Map<String, BaseModel> collectingAndThen = baseList.stream().collect(Collectors.groupingBy(
                BaseModel::getSex, Collectors.collectingAndThen
                        (Collectors.minBy(Comparator.comparing(BaseModel::getAge)), Optional::get)));
        System.out.println(collectingAndThen);*/
        //分组5
        /*Map<String, Double> averagingInt = baseList.stream().collect(Collectors.groupingBy(
                BaseModel::getSex, Collectors.averagingInt(BaseModel::getAge)));
        System.out.println(averagingInt);*/
        //分组6 分组
        /*Map<String, Set<String>> mappingToSet = baseList.stream().collect(Collectors.groupingBy(BaseModel::getSex, Collectors.mapping(
                baseModel -> {
                    if (baseModel.getAge() > 30) {
                        return baseModel.getName() + ">30";
                    } else {
                        return baseModel.getName() + "<30";
                    }
                }, Collectors.toSet())));
        System.out.println(mappingToSet);*/
        //分组7
        /*Map<String, ArrayList<String>> mapping2Collection = baseList.stream().collect(Collectors.groupingBy(BaseModel::getSex, Collectors.mapping(baseModel ->
        {
            if (baseModel.getAge() > 30) {
                return baseModel.getName() + ">30";
            } else {
                return baseModel.getName() + "<30";
            }
        }, Collectors.toCollection(ArrayList::new))));
        System.out.println(mapping2Collection);*/
        //规约
        /*Long collect = baseList.stream().collect(Collectors.counting());
        long count = baseList.stream().count();
        System.out.println(count + "====" + collect);*/
        //求最大值和最小值
        /*Optional<BaseModel> comparingMax = baseList.stream().collect(Collectors.maxBy(Comparator.comparing(BaseModel::getAge)));
        Optional<BaseModel> comparingMin = baseList.stream().collect(Collectors.minBy(Comparator.comparing(BaseModel::getAge)));
        if (comparingMax.isPresent()){
            System.out.println("max =" + comparingMax.get().getAge());
        }
        if (comparingMin.isPresent()){
            System.out.println("min =" + comparingMin.get().getAge());
        }*/
        //汇总
        //求和
        /*Integer summingInt = baseList.stream().collect(Collectors.summingInt(BaseModel::getAge));
        System.out.println("summingInt =" + summingInt);*/
        //求平均值
        /*Double averaging = baseList.stream().collect(Collectors.averagingInt(BaseModel::getAge));
        System.out.println("averaging =" + averaging);*/
        //summarizing......   可以求和、平均数、最大、最小、数据数量  放入到一个 ....SummaryStatistics 对象中
        /*IntSummaryStatistics totalStatistics = baseList.stream().collect(Collectors.summarizingInt(BaseModel::getAge));
        System.out.println("getAverage=" + totalStatistics.getAverage() + "  getCount=" + totalStatistics.getCount()
                + "    getMax="  + totalStatistics.getMax() + "  getMin=" + totalStatistics.getMin()
                + "  getSum=" + totalStatistics.getSum());*/
        // joining 连接字符串  会将流中每一个对象调用toString方法连接起来，可以自定义分隔符号
        /*String nameStrJoin = baseList.stream().map(BaseModel::getName).collect(Collectors.joining(" "));
        System.out.println(nameStrJoin);*/
        //广义上的规约
        /*Optional<String> reduce = baseList.stream().map(BaseModel::getName).reduce((a, b) -> a + "," + b);
        if (reduce.isPresent()){
            System.out.println("reduce =" + reduce.get());
        }
        // reducing进行累加                                                                     //起始值  所需操作对象      进行的操作
        String collectReducing = baseList.stream().collect(Collectors.reducing("", BaseModel::getName, (a, b) -> a + "," + b));
        System.out.println("collectReducing=" + collectReducing);*/
        //求最大值
        /*Optional<BaseModel> maxValue = baseList.stream().collect(Collectors.reducing((a, b) -> (a.getAge() > b.getAge()) ? a : b));
        if (maxValue.isPresent()){
            System.out.println("maxAgeValueByReduce=" + maxValue.get().getAge());
        }*/
        //用reducing来实现Collectors.toList()
        /*Stream<Integer> stream = Arrays.asList(1, 2, 3, 4, 5, 6).stream();
        List<Integer> toListByReduce = stream.reduce(new ArrayList<>(),
                (List<Integer> l1, Integer a) -> {
                    l1.add(a);
                    return l1;
                }, (List<Integer> l2, List<Integer> l3) -> {
                    l2.addAll(l3);
                    return l2;
                });
        System.out.println("toListByReduce =" + toListByReduce);*/
    }


    static List<BaseModel> baseList;

    static {
        baseList = new ArrayList<BaseModel>();
        baseList.add(new BaseModel("刘华",26,"男"));
        baseList.add(new BaseModel("刘华",26,"男"));
        baseList.add(new BaseModel("刘已",26,"女"));
        baseList.add(new BaseModel("是啥",56,"男"));
        baseList.add(new BaseModel("自行车",16,"女"));
        baseList.add(new BaseModel("秩序册",89,"男"));
        baseList.add(new BaseModel("儿女",42,"男"));
        baseList.add(new BaseModel("瞅瞅",19,"女"));
        baseList.add(new BaseModel("分隔",52,"男"));
        baseList.add(new BaseModel("四渡赤水",46,"女"));
        //baseList.add(new BaseModel("撒旦法",41,"男"));
    }

}
